export const setKeyTitle = (treeData) => {
  treeData.forEach((item) => {
    if (item.children) {
      item.key = item.code;
      item.title = item.name;
      setKeyTitle(item.children);
    } else {
      item.key = item.code;
      item.title = item.name;
    }
  });
  return treeData;
};

export const setLabelValue = (treeData) => {
  treeData.forEach((item) => {
    if (item.children) {
      item.value = item.code;
      item.label = item.name;
      setLabelValue(item.children);
    } else {
      item.value = item.code;
      item.label = item.name;
    }
  });
  return treeData;
};

export const formatTree = (data, parentCode) => {
  let result = [];
  data.forEach((item) => {
    if (item.parentCode === parentCode) {
      let children = formatTree(data, item.code);
      if (children.length === 0) {
        children = null;
      }
      item.children = children;
      result.push(item);
    }
  });
  return result;
};
